﻿#include "SaveDevToModelFile.h"
#include "ui_SaveDevToModelFile.h"
#include <QMessageBox>
#include <QFile>
#include <QAbstractButton>

SaveDevToModelFile::SaveDevToModelFile(int AppType, int TableId, unsigned int RecordId, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SaveDevToModelFile)
{
    ui->setupUi(this);
	ui->pModelNameEdit->setReadOnly(true);
	m_AppType =0;
	m_TableId =0;
	m_RecordId =0;

	m_DevYxNum =0;
	m_DevYcNum =0;
	m_DevYkNum =0;
	m_DevYmNum =0;
// 	m_DevFixValNum =0;
// 	m_DevRlySOENum =0;
// 	m_DevCheckSelfNum =0;
// 	m_DevRlySamplingNum =0;
	m_AppType = AppType;
	m_TableId = TableId;
	m_RecordId = RecordId;
	
	setWindowTitle(QString::fromLocal8Bit("保存设备为模板文件"));
	connect(ui->pAffirmBtn, SIGNAL(clicked()), this, SLOT(AffirmBtn()));
	connect(ui->pCancelBtn, SIGNAL(clicked()), this, SLOT(close()));
	InitShow();
}

SaveDevToModelFile::~SaveDevToModelFile()
{
    delete ui;
}

void SaveDevToModelFile::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void SaveDevToModelFile::InitShow()
{
	if (SYSTEM_TYPE_NSA_YD == m_AppType && 
		TABLE_RDEVMODEL_INFO == m_TableId &&
		m_RecordId >0)
	{
		char	*buff = NULL;
		int	buff_len = 0;
		int	RetVal = 0;
		RetVal = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_RecordId(m_AppType, m_TableId, m_RecordId, buff, &buff_len);
		if (RetVal>0 && buff)
		{
			m_DevName = QString::fromLocal8Bit(((rDevModel_Info*)buff)->Name);
		}
		if (buff)
		{
			delete [](char*)buff;
			buff = NULL;
		}
		ui->pModelNameEdit->setText(m_DevName);
		m_ModelFileName = m_DevName;
		ui->pModelFileEdit->setText(m_ModelFileName);
	}
}

void SaveDevToModelFile::AffirmBtn()
{
	QString FileName;
	FileName = ui->pModelFileEdit->text();
	m_ModelFileName = FileName;
	if (m_ModelFileName.length()<=0)
	{
		QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("请输入模板文件名！"), QMessageBox::Ok | QMessageBox::Cancel);
		return ;
	}
		
	QString dirStr;
//	dirStr = DirMgr::GetSysDirAllEn_Name(SYSTEM_DIR_S_LOG);
	//	dirStr.replace(QString("s_log"), QString("db"));
	dirStr  = QString::fromLocal8Bit(DB_CFG_DEFFILE_PATH);
	if(!QDir(dirStr).exists())
	{
		QDir Dir;
		Dir.mkpath(dirStr);
	}
	m_ModelAllFileName = dirStr+m_ModelFileName+QString::fromLocal8Bit(".def");
	if (QFile::exists(m_ModelAllFileName))
	{
		QMessageBox Msg(QMessageBox::Question, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("模板文件已经存在，是否覆盖？"));
		QAbstractButton *pYesBtn = (QAbstractButton *)Msg.addButton(QString::fromLocal8Bit("是"), QMessageBox::YesRole);
		QAbstractButton *pNoBtn = (QAbstractButton *)Msg.addButton(QString::fromLocal8Bit("否"), QMessageBox::NoRole);
		Msg.exec();
		if (Msg.clickedButton() != pYesBtn)
		{
			return;
		}
		QFile createFile(m_ModelAllFileName);
		createFile.remove(m_ModelAllFileName);
		createFile.close();
	}

	if (SaveDeviceFile()<0)
	{
		QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("模板文件保存出错!"), QMessageBox::Ok);
		return ;
	}
}

int SaveDevToModelFile::SaveDeviceFile()
{
	
	if (SYSTEM_TYPE_NSA_YD == m_AppType && 
		TABLE_RDEVMODEL_INFO == m_TableId &&
		m_RecordId >0)
	{
		bool			bRet = false;
		QString			SectName;
		QString			KeyName;
		QString			strValue;
//		g_DbCfgFunObj.GetRecordNumbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YX, m_RecordId+TABLE_RDEVMODEL_INFO*PARENT_OFFSET, &m_DevYxNum);
//		g_DbCfgFunObj.GetRecordNumbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YC, m_RecordId+TABLE_RDEVMODEL_INFO*PARENT_OFFSET, &m_DevYcNum);
//		g_DbCfgFunObj.GetRecordNumbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YK, m_RecordId+TABLE_RDEVMODEL_INFO*PARENT_OFFSET, &m_DevYkNum);
//		g_DbCfgFunObj.GetRecordNumbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YM, m_RecordId+TABLE_RDEVMODEL_INFO*PARENT_OFFSET, &m_DevYmNum);
		
		//SectName = QString::fromLocal8Bit("NSA3000T");
		SectName = m_ModelFileName;
		//KeyName = QString("0");
		//sprintf(m_strBuff, "YX, YC, YK, YM"); 
		//strValue = QString::fromLocal8Bit(m_strBuff);
		//bRet = m_FileOper.WritePrivateProfileString_My(SectName, KeyName, strValue, m_ModelAllFileName);
		//if (!bRet)
		//{
		//	return 0;
		//}
		//sprintf(m_strBuff, "%d	%d	%d	%d",
		//	m_DevYxNum,
		//	m_DevYcNum,
		//	m_DevYkNum,
		//	m_DevYmNum/*,
		//	m_DevFixValNum,
		//	m_DevRlySOENum,
		//	m_DevCheckSelfNum,
		//	m_DevRlySamplingNum*/);

		//strValue = QString::fromLocal8Bit(m_strBuff);
		//KeyName = QString("1");

		//bRet = m_FileOper.WritePrivateProfileString_My(SectName, KeyName, strValue, m_ModelAllFileName);
		//if (!bRet)
		//{
		//	return 0;
		//}
		QFile			writeFile(m_ModelAllFileName);
		QTextStream		writeStream;

		writeStream.setCodec("gbk");

		//文件不存在，则创建，且直接写入
		if(!QFile::exists(m_ModelAllFileName))
		{
			if(!writeFile.open(QFile::WriteOnly | QIODevice::Text))
			{
				QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("无法打开指定文件!"), QMessageBox::Ok);
				return -1;
			}
			//如果key和value不是null，才写内容
			if(!SectName.isEmpty())
			{
				writeStream.setDevice(&writeFile);
				writeStream<<SectName<<QString::fromLocal8Bit(" 信息表")<<"\n";
				writeStream<<QString::fromLocal8Bit("遥信量:点号,名称,FUN,INF,遥信类型")<<"\n";
				writeStream<<QString::fromLocal8Bit("软压板:点号,名称,FUN,INF,遥信类型")<<"\n";
				writeStream<<QString::fromLocal8Bit("遥测量:点号,名称,FUN,INF,额定值(int),百分比(int),越限延时(int),比例因子(float),遥测高限(float),遥测低限(float),单位")<<"\n";
				writeStream<<QString::fromLocal8Bit("保护测量:点号,名称,FUN,INF,额定值(int),百分比(int),越限延时(int),比例因子(float),遥测高限(float),遥测低限(float),单位")<<"\n";
				writeStream<<QString::fromLocal8Bit("遥控量:点号,名称,FUN,INF")<<"\n";
				writeStream<<QString::fromLocal8Bit("遥脉量:点号,名称,FUN,INF")<<"\n";
				writeStream<<QString::fromLocal8Bit("定值:点号,名称,单位,FUN,INF,缺省值,最小值,最大值,数据格式,数据类型,步长")<<"\n";
				writeStream<<QString::fromLocal8Bit("参数:点号,名称,单位,FUN,INF,缺省值,最小值,最大值,数据格式,数据类型,步长")<<"\n";
				writeStream<<QString::fromLocal8Bit("精度系数:点号,名称,FUN,INF,缺省值,最小值,最大值,数据类型")<<"\n";

				writeStream.flush();
			}
			writeFile.close();
		}

		int newYxNum = SaveDeviceYx_toFile(m_RecordId);
		int newYcNum = SaveDeviceYc_toFile(m_RecordId);
		int newYkNum = SaveDeviceYk_toFile(m_RecordId);
		int newYmNum = SaveDeviceYm_toFile(m_RecordId);
		int newBhYcNum = SaveDeviceBhYc_toFile(m_RecordId);
		int newSetNum = SaveModelSet_toFile(m_RecordId);
		int newParaNum = SaveModelPara_toFile(m_RecordId);
		int newPrecNum = SaveModelPrec_toFile(m_RecordId);
		int newAreaNum = SaveModelArea_toFile(m_RecordId);
		int newRybNum = SaveRYB_toFile(m_RecordId);
		
		QString Str;
		Str = QString::fromLocal8Bit("设备模板文件“%1”: \r\n YxNum:%2,	YcNum:%3\r\n YkNum:%4,	YmNum:%5\r\n BhYcNum:%6,	SetNum:%7\r\n ParaNum:%8,	PrecNum:%9\r\n AreaNum:%10 RybNum:%11")
			.arg(m_ModelAllFileName)
			.arg(newYxNum)
			.arg(newYcNum)
			.arg(newYkNum)
			.arg(newYmNum)
			.arg(newBhYcNum)
			.arg(newSetNum)
			.arg(newParaNum)
			.arg(newPrecNum)
			.arg(newAreaNum)
			.arg(newRybNum);

		QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), Str, QMessageBox::Ok | QMessageBox::Cancel);
		return 1;
	}
	
	return -1;
}

int SaveDevToModelFile::SaveRYB_toFile(unsigned int Device_Id)
{
	if (Device_Id<=0)
	{
		return 0;
	}
	char		    *buff = NULL;
	int			    buff_len = 0;
	int			    Ret = 0;
	int			    i = 0;
	int			    newYxNum = 0;
	rDevModel_Ryb 	rRybInfo;
	bool			bRet = false;
	QString			SectName;
	QString			KeyName;
	QString			strValue;

	SectName = QString::fromLocal8Bit("软压板");

	QFile			writeFile(m_ModelAllFileName);
	QTextStream		writeStream;

	qint64			KeyPos = 0;

	writeStream.setCodec("gbk");

	if(QFile::exists(m_ModelAllFileName))
	{
		if(!writeFile.open(QFile::ReadWrite | QIODevice::Text))
		{
			QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("无法打开指定文件!"), QMessageBox::Ok);
			return -1;
		}
		if(!SectName.isEmpty())
		{
			writeStream.setDevice(&writeFile);	
			KeyPos = writeFile.size();
			writeStream.seek(KeyPos);
			writeStream<<"["<<SectName<<"] "<<"\n";
			writeStream.flush();//写入流到文件
			writeFile.close();
		}
		else
		{
			writeFile.close();
			return -3;
		}
	}
	else
	{
		return -2;
	}

	Ret = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_RYB, Device_Id+TABLE_RDEVMODEL_INFO*PARENT_OFFSET, buff, &buff_len, true);
	if (Ret>0 && buff)
	{
		for (i=0; i<Ret; i++)
		{
			memcpy(&rRybInfo, buff+i*sizeof(rDevModel_Ryb), sizeof(rDevModel_Ryb));
			memset(m_strBuff, 0, TEMP_BUFF_LEN);
			sprintf(m_strBuff, "%d,%s,%d,%d", 
				rRybInfo.IndexNo,
				rRybInfo.Name,						//名称
				rRybInfo.Fun,					//点号
				rRybInfo.Inf);
			strValue = QString::fromLocal8Bit(m_strBuff);
			KeyName = QString("%1").arg(i+1);

			bRet = m_FileOper.WritePrivateProfileString_My(SectName, KeyName, strValue, m_ModelAllFileName);
			if (!bRet)
			{
				break;
			}
			newYxNum++;
		}
	}

	if (buff)
	{
		delete [](char*)buff;
		buff = NULL;
	}
	return newYxNum;
}

//	保存模板YX记录
int SaveDevToModelFile::SaveDeviceYx_toFile(unsigned int Device_Id)
{
	if (Device_Id<=0)
	{
		return 0;
	}
	char		    *buff = NULL;
	int			    buff_len = 0;
	int			    Ret = 0;
	int			    i = 0;
	int			    newYxNum = 0;
	rDevModel_Yx 	rYxInfo;
	bool			bRet = false;
	QString			SectName;
	QString			KeyName;
	QString			strValue;
	
	SectName = QString::fromLocal8Bit("遥信量");
// 	KeyName = QString("0");
// 	sprintf(m_strBuff, "名称	点号	FUN	INF	组号	条目号"); 
// 	strValue = QString::fromLocal8Bit(m_strBuff);
// 	bRet = m_FileOper.WritePrivateProfileString_My(SectName, KeyName, strValue, m_ModelAllFileName);
// 	if (!bRet)
// 	{
// 		return 0;
	// 	}

	QFile			writeFile(m_ModelAllFileName);
	QTextStream		writeStream;

	qint64			KeyPos = 0;

	writeStream.setCodec("gbk");

	if(QFile::exists(m_ModelAllFileName))
	{
		if(!writeFile.open(QFile::ReadWrite | QIODevice::Text))
		{
			QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("无法打开指定文件!"), QMessageBox::Ok);
			return -1;
		}
		if(!SectName.isEmpty())
		{
			writeStream.setDevice(&writeFile);	
			KeyPos = writeFile.size();
			writeStream.seek(KeyPos);
			writeStream<<"["<<SectName<<"] "<<"\n";
			writeStream.flush();//写入流到文件
			writeFile.close();
		}
		else
		{
			writeFile.close();
			return -3;
		}
	}
	else
	{
		return -2;
	}

	Ret = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YX, Device_Id+TABLE_RDEVMODEL_INFO*PARENT_OFFSET, buff, &buff_len, true);
	if (Ret>0 && buff)
	{
		for (i=0; i<Ret; i++)
		{
			memcpy(&rYxInfo, buff+i*sizeof(rDevModel_Yx), sizeof(rDevModel_Yx));
			memset(m_strBuff, 0, TEMP_BUFF_LEN);
			sprintf(m_strBuff, "%d,%s,%d,%d,%d ",
				rYxInfo.IndexNo,
				rYxInfo.Name,						//名称
				rYxInfo.Fun,					//点号
				rYxInfo.Inf,						//FUN		
				rYxInfo.YxType);
			strValue = QString::fromLocal8Bit(m_strBuff);
			KeyName = QString("%1").arg(i+1);

			bRet = m_FileOper.WritePrivateProfileString_My(SectName, KeyName, strValue, m_ModelAllFileName);
			if (!bRet)
			{
				break;
			}
			newYxNum++;
		}
	}

	if (buff)
	{
		delete [](char*)buff;
		buff = NULL;
	}
	return newYxNum;
}

//	保存模板Yk记录
int SaveDevToModelFile::SaveDeviceYc_toFile(unsigned int Device_Id)
{
	if (Device_Id<=0)
	{
		return 0;
	}
	char			*buff = NULL;
	int			buff_len = 0;
	int			Ret = 0;
	int			i = 0;
	int			newYcNum = 0;
	rDevModel_Yc	rYcInfo;
	bool			bRet = false;
	QString			SectName;
	QString			KeyName;
	QString			strValue;

	SectName = QString::fromLocal8Bit("遥测量");
// 	KeyName = QString("0");
// 	sprintf(m_strBuff, "名称	点号 FUN	INF	组号	条目号	单位	比例因子	偏移量"); 
// 	strValue = QString::fromLocal8Bit(m_strBuff);
// 	bRet = m_FileOper.WritePrivateProfileString_My(SectName, KeyName, strValue, m_ModelAllFileName);
// 	if (!bRet)
// 	{
// 		return 0;
	// 	}

	QFile			writeFile(m_ModelAllFileName);
	QTextStream		writeStream;

	qint64			KeyPos = 0;

	writeStream.setCodec("gbk");

	if(QFile::exists(m_ModelAllFileName))
	{
		if(!writeFile.open(QFile::ReadWrite | QIODevice::Text))
		{
			QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("无法打开指定文件!"), QMessageBox::Ok);
			return -1;
		}
		if(!SectName.isEmpty())
		{
			writeStream.setDevice(&writeFile);	
			KeyPos = writeFile.size();
			writeStream.seek(KeyPos);
			writeStream<<"["<<SectName<<"] "<<"\n";
			writeStream.flush();//写入流到文件
			writeFile.close();
		}
		else
		{
			writeFile.close();
			return -3;
		}
	}
	else
	{
		return -2;
	}

	Ret = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YC, Device_Id+TABLE_RDEVMODEL_INFO*PARENT_OFFSET, buff, &buff_len, true);
	if (Ret>0 && buff)
	{
		for (i=0; i<Ret; i++)
		{
			memcpy(&rYcInfo, buff+i*sizeof(rDevModel_Yc), sizeof(rDevModel_Yc));
			memset(m_strBuff, 0, TEMP_BUFF_LEN);

			sprintf(m_strBuff, "%d,%s,%d,%d,%d,%d,%d,%0.1f,%0.1f,%0.1f,%s ", 
				rYcInfo.IndexNo,
				rYcInfo.Name,						//名称
				rYcInfo.Fun,						//FUN		
				rYcInfo.Inf,						//INF
				int(rYcInfo.rating),						//点号
				rYcInfo.percent,					//GroupNo
				rYcInfo.tYCLimit,
				rYcInfo.ratio,
				rYcInfo.High_limit,
				rYcInfo.Low_limit,
				rYcInfo.unit);
			strValue = QString::fromLocal8Bit(m_strBuff);
			KeyName = QString("%1").arg(i+1);
			bRet = m_FileOper.WritePrivateProfileString_My(SectName, KeyName, strValue, m_ModelAllFileName);
			if (!bRet)
			{
				break;
			}
			newYcNum++;
		}
	}

	if (buff)
	{
		delete [](char*)buff;
		buff = NULL;
	}
	return newYcNum;
}


//	保存模板Yk记录
int SaveDevToModelFile::SaveDeviceBhYc_toFile(unsigned int Device_Id)
{
	if (Device_Id<=0)
	{
		return 0;
	}
	char			*buff = NULL;
	int			buff_len = 0;
	int			Ret = 0;
	int			i = 0;
	int			newYcNum = 0;
	rDevModel_Bhyc	rYcInfo;
	bool			bRet = false;
	QString			SectName;
	QString			KeyName;
	QString			strValue;

	SectName = QString::fromLocal8Bit("保护测量");

	QFile			writeFile(m_ModelAllFileName);
	QTextStream		writeStream;

	qint64			KeyPos = 0;

	writeStream.setCodec("gbk");

	if(QFile::exists(m_ModelAllFileName))
	{
		if(!writeFile.open(QFile::ReadWrite | QIODevice::Text))
		{
			QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("无法打开指定文件!"), QMessageBox::Ok);
			return -1;
		}
		if(!SectName.isEmpty())
		{
			writeStream.setDevice(&writeFile);	
			KeyPos = writeFile.size();
			writeStream.seek(KeyPos);
			writeStream<<"["<<SectName<<"] "<<"\n";
			writeStream.flush();//写入流到文件
			writeFile.close();
		}
		else
		{
			writeFile.close();
			return -3;
		}
	}
	else
	{
		return -2;
	}

	Ret = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_BHYC, Device_Id+TABLE_RDEVMODEL_INFO*PARENT_OFFSET, buff, &buff_len, true);
	if (Ret>0 && buff)
	{
		for (i=0; i<Ret; i++)
		{
			memcpy(&rYcInfo, buff+i*sizeof(rDevModel_Bhyc), sizeof(rDevModel_Bhyc));
			memset(m_strBuff, 0, TEMP_BUFF_LEN);

			sprintf(m_strBuff, "%d,%s,%d,%d,%d,%d,%d,%0.1f,%0.1f,%0.1f,%s ", 
				rYcInfo.IndexNo,
				rYcInfo.Name,						//名称
				rYcInfo.Fun,						//FUN		
				rYcInfo.Inf,						//INF
				int(rYcInfo.rating),						//点号
				rYcInfo.percent,					//GroupNo
				rYcInfo.tYCLimit,
				rYcInfo.ratio,
				rYcInfo.High_limit,
				rYcInfo.Low_limit,
				rYcInfo.unit);
			strValue = QString::fromLocal8Bit(m_strBuff);
			KeyName = QString("%1").arg(i+1);
			bRet = m_FileOper.WritePrivateProfileString_My(SectName, KeyName, strValue, m_ModelAllFileName);
			if (!bRet)
			{
				break;
			}
			newYcNum++;
		}
	}

	if (buff)
	{
		delete [](char*)buff;
		buff = NULL;
	}
	return newYcNum;
}

//	保存模板YX记录
int SaveDevToModelFile::SaveDeviceYk_toFile(unsigned int Device_Id)
{
	if (Device_Id<=0)
	{
		return 0;
	}
	char			*buff = NULL;
	int			buff_len = 0;
	int			Ret = 0;
	int			i = 0;
	int			newYkNum = 0;
	rDevModel_Yk	rYkInfo;
	bool			bRet = false;
	QString			SectName;
	QString			KeyName;
	QString			strValue;

	SectName = QString::fromLocal8Bit("遥控量");
// 	KeyName = QString("0");
// 	sprintf(m_strBuff, "名称	点号	FUN	INF	组号	条目号"); 
// 	strValue = QString::fromLocal8Bit(m_strBuff);
// 	bRet = m_FileOper.WritePrivateProfileString_My(SectName, KeyName, strValue, m_ModelAllFileName);
// 	if (!bRet)
// 	{
// 		return 0;
	// 	}

	QFile			writeFile(m_ModelAllFileName);
	QTextStream		writeStream;

	qint64			KeyPos = 0;

	writeStream.setCodec("gbk");

	if(QFile::exists(m_ModelAllFileName))
	{
		if(!writeFile.open(QFile::ReadWrite | QIODevice::Text))
		{
			QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("无法打开指定文件!"), QMessageBox::Ok);
			return -1;
		}
		if(!SectName.isEmpty())
		{
			writeStream.setDevice(&writeFile);	
			KeyPos = writeFile.size();
			writeStream.seek(KeyPos);
			writeStream<<"["<<SectName<<"] "<<"\n";
			writeStream.flush();//写入流到文件
			writeFile.close();
		}
		else
		{
			writeFile.close();
			return -3;
		}
	}
	else
	{
		return -2;
	}

	Ret = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YK, Device_Id+TABLE_RDEVMODEL_INFO*PARENT_OFFSET, buff, &buff_len, true);
	if (Ret>0 && buff)
	{
		for (i=0; i<Ret; i++)
		{
			memcpy(&rYkInfo, buff+i*sizeof(rDevModel_Yk), sizeof(rDevModel_Yk));
			memset(m_strBuff, 0, TEMP_BUFF_LEN);
			sprintf(m_strBuff, "%d,%s,%d,%d ", 
				rYkInfo.IndexNo,
				rYkInfo.Name,						//名称
				rYkInfo.Fun,						//FUN		
				rYkInfo.Inf);
			strValue = QString::fromLocal8Bit(m_strBuff);
			KeyName = QString("%1").arg(i+1);
			bRet = m_FileOper.WritePrivateProfileString_My(SectName, KeyName, strValue, m_ModelAllFileName);
			if (!bRet)
			{
				break;
			}
			newYkNum++;
		}
	}

	if (buff)
	{
		delete [](char*)buff;
		buff = NULL;
	}
	return newYkNum;
}

//	保存模板Ym记录
int SaveDevToModelFile::SaveDeviceYm_toFile(unsigned int Device_Id)
{
	if (Device_Id<=0)
	{
		return 0;
	}
	char			*buff = NULL;
	int			buff_len = 0;
	int			Ret = 0;
	int			i = 0;
	int			newYmNum = 0;
	rDevModel_Ym	rYmInfo;
	bool			bRet = false;
	QString			SectName;
	QString			KeyName;
	QString			strValue;

	SectName = QString::fromLocal8Bit("遥脉量");

	QFile			writeFile(m_ModelAllFileName);
	QTextStream		writeStream;

	qint64			KeyPos = 0;

	writeStream.setCodec("gbk");

	if(QFile::exists(m_ModelAllFileName))
	{
		if(!writeFile.open(QFile::ReadWrite | QIODevice::Text))
		{
			QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("无法打开指定文件!"), QMessageBox::Ok);
			return -1;
		}
		if(!SectName.isEmpty())
		{
			writeStream.setDevice(&writeFile);	
			KeyPos = writeFile.size();
			writeStream.seek(KeyPos);
			writeStream<<"["<<SectName<<"] "<<"\n";
			writeStream.flush();//写入流到文件
			writeFile.close();
		}
		else
		{
			writeFile.close();
			return -3;
		}
	}
	else
	{
		return -2;
	}

	Ret = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_YM, Device_Id+TABLE_RDEVMODEL_INFO*PARENT_OFFSET, buff, &buff_len, true);
	if (Ret>0 && buff)
	{
		for (i=0; i<Ret; i++)
		{
			memcpy(&rYmInfo, buff+i*sizeof(rDevModel_Ym), sizeof(rDevModel_Ym));
			memset(m_strBuff, 0, TEMP_BUFF_LEN);
			sprintf(m_strBuff, "%d,%s,%d,%d ", 
				rYmInfo.IndexNo,
				rYmInfo.Name,						//名称
				rYmInfo.Fun,						//FUN		
				rYmInfo.Inf);
			strValue = QString::fromLocal8Bit(m_strBuff);
			KeyName = QString("%1").arg(i+1);
			bRet = m_FileOper.WritePrivateProfileString_My(SectName, KeyName, strValue, m_ModelAllFileName);
			if (!bRet)
			{
				break;
			}
			newYmNum++;
		}
	}

	if (buff)
	{
		delete [](char*)buff;
		buff = NULL;
	}
	return newYmNum;
}

int SaveDevToModelFile::SaveModelSet_toFile(unsigned int Device_Id)
{
	if (Device_Id<=0)
	{
		return 0;
	}
	char			*buff = NULL;
	int			buff_len = 0;
	int			Ret = 0;
	int			i = 0;
	int			newSetNum = 0;
	rDevModel_Set	rSetInfo;
	bool			bRet = false;
	QString			SectName;
	QString			KeyName;
	QString			strValue;

	SectName = QString::fromLocal8Bit("定值");

	QFile			writeFile(m_ModelAllFileName);
	QTextStream		writeStream;

	qint64			KeyPos = 0;

	writeStream.setCodec("gbk");

	if(QFile::exists(m_ModelAllFileName))
	{
		if(!writeFile.open(QFile::ReadWrite | QIODevice::Text))
		{
			QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("无法打开指定文件!"), QMessageBox::Ok);
			return -1;
		}
		if(!SectName.isEmpty())
		{
			writeStream.setDevice(&writeFile);	
			KeyPos = writeFile.size();
			writeStream.seek(KeyPos);
			writeStream<<"["<<SectName<<"] "<<"\n";
			writeStream.flush();//写入流到文件
			writeFile.close();
		}
		else
		{
			writeFile.close();
			return -3;
		}
	}
	else
	{
		return -2;
	}

	Ret = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_SET, Device_Id+TABLE_RDEVMODEL_INFO*PARENT_OFFSET, buff, &buff_len, true);
	if (Ret>0 && buff)
	{
		for (i=0; i<Ret; i++)
		{
			memcpy(&rSetInfo, buff+i*sizeof(rDevModel_Set), sizeof(rDevModel_Set));
			memset(m_strBuff, 0, TEMP_BUFF_LEN);
			sprintf(m_strBuff, "%d,%s,%s,%d,%d,%0.2f,%0.2f,%0.2f,%d,%d,%0.2f ", 
				rSetInfo.IndexNo,
				rSetInfo.Name,						//名称
				rSetInfo.unit,
				rSetInfo.Fun,						//FUN		
				rSetInfo.Inf,
				rSetInfo.defval,
				rSetInfo.min_val,
				rSetInfo.max_val,
				rSetInfo.daFormat,
				rSetInfo.daType,
				rSetInfo.step_val);
			
			strValue = QString::fromLocal8Bit(m_strBuff);
			KeyName = QString("%1").arg(i+1);
			bRet = m_FileOper.WritePrivateProfileString_My(SectName, KeyName, strValue, m_ModelAllFileName);
			if (!bRet)
			{
				break;
			}
			newSetNum++;
		}
	}

	if (buff)
	{
		delete [](char*)buff;
		buff = NULL;
	}
	return newSetNum;
}


int SaveDevToModelFile::SaveModelPara_toFile(unsigned int Device_Id)
{
	if (Device_Id<=0)
	{
		return 0;
	}
	char			*buff = NULL;
	int				buff_len = 0;
	int				Ret = 0;
	int				i = 0;
	int				newParaNum = 0;
	rDevModel_Para	rParaInfo;
	bool			bRet = false;
	QString			SectName;
	QString			KeyName;
	QString			strValue;

	SectName = QString::fromLocal8Bit("参数");

	QFile			writeFile(m_ModelAllFileName);
	QTextStream		writeStream;

	qint64			KeyPos = 0;

	writeStream.setCodec("gbk");

	if(QFile::exists(m_ModelAllFileName))
	{
		if(!writeFile.open(QFile::ReadWrite | QIODevice::Text))
		{
			QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("无法打开指定文件!"), QMessageBox::Ok);
			return -1;
		}
		if(!SectName.isEmpty())
		{
			writeStream.setDevice(&writeFile);	
			KeyPos = writeFile.size();
			writeStream.seek(KeyPos);
			writeStream<<"["<<SectName<<"] "<<"\n";
			writeStream.flush();//写入流到文件
			writeFile.close();
		}
		else
		{
			writeFile.close();
			return -3;
		}
	}
	else
	{
		return -2;
	}

	Ret = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_PARA, Device_Id+TABLE_RDEVMODEL_INFO*PARENT_OFFSET, buff, &buff_len, true);
	if (Ret>0 && buff)
	{
		for (i=0; i<Ret; i++)
		{
			memcpy(&rParaInfo, buff+i*sizeof(rDevModel_Para), sizeof(rDevModel_Para));
			memset(m_strBuff, 0, TEMP_BUFF_LEN);
			sprintf(m_strBuff, "%d,%s,%s,%d,%d,%0.2f,%0.2f,%0.2f,%d,%d,%0.2f ", 
				rParaInfo.IndexNo,
				rParaInfo.Name,						//名称
				rParaInfo.unit,
				rParaInfo.Fun,						//FUN		
				rParaInfo.Inf,
				rParaInfo.defval,
				rParaInfo.min_val,
				rParaInfo.max_val,
				rParaInfo.daFormat,
				rParaInfo.daType,
				rParaInfo.step_val);

			strValue = QString::fromLocal8Bit(m_strBuff);
			KeyName = QString("%1").arg(i+1);
			bRet = m_FileOper.WritePrivateProfileString_My(SectName, KeyName, strValue, m_ModelAllFileName);
			if (!bRet)
			{
				break;
			}
			newParaNum++;
		}
	}

	if (buff)
	{
		delete [](char*)buff;
		buff = NULL;
	}
	return newParaNum;
}

int SaveDevToModelFile::SaveModelPrec_toFile(unsigned int Device_Id)
{
	if (Device_Id<=0)
	{
		return 0;
	}
	char			*buff = NULL;
	int				buff_len = 0;
	int				Ret = 0;
	int				i = 0;
	int				newPrecNum = 0;
	rDevModel_Prec	rPrecInfo;
	bool			bRet = false;
	QString			SectName;
	QString			KeyName;
	QString			strValue;

	SectName = QString::fromLocal8Bit("精度");

	QFile			writeFile(m_ModelAllFileName);
	QTextStream		writeStream;

	qint64			KeyPos = 0;

	writeStream.setCodec("gbk");

	if(QFile::exists(m_ModelAllFileName))
	{
		if(!writeFile.open(QFile::ReadWrite | QIODevice::Text))
		{
			QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("无法打开指定文件!"), QMessageBox::Ok);
			return -1;
		}
		if(!SectName.isEmpty())
		{
			writeStream.setDevice(&writeFile);	
			KeyPos = writeFile.size();
			writeStream.seek(KeyPos);
			writeStream<<"["<<SectName<<"] "<<"\n";
			writeStream.flush();//写入流到文件
			writeFile.close();
		}
		else
		{
			writeFile.close();
			return -3;
		}
	}
	else
	{
		return -2;
	}

	Ret = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_PREC, Device_Id+TABLE_RDEVMODEL_INFO*PARENT_OFFSET, buff, &buff_len, true);
	if (Ret>0 && buff)
	{
		for (i=0; i<Ret; i++)
		{
			memcpy(&rPrecInfo, buff+i*sizeof(rDevModel_Prec), sizeof(rDevModel_Prec));
			memset(m_strBuff, 0, TEMP_BUFF_LEN);
			sprintf(m_strBuff, "%d,%s,%d,%d,%0.2f,%0.2f,%0.2f ", 
				rPrecInfo.IndexNo,
				rPrecInfo.Name,						//名称
				rPrecInfo.Fun,						//FUN		
				rPrecInfo.Inf,
				rPrecInfo.defval,
				rPrecInfo.min_val,
				rPrecInfo.max_val);

			strValue = QString::fromLocal8Bit(m_strBuff);
			KeyName = QString("%1").arg(i+1);
			bRet = m_FileOper.WritePrivateProfileString_My(SectName, KeyName, strValue, m_ModelAllFileName);
			if (!bRet)
			{
				break;
			}
			newPrecNum++;
		}
	}

	if (buff)
	{
		delete [](char*)buff;
		buff = NULL;
	}
	return newPrecNum;
}


int SaveDevToModelFile::SaveModelArea_toFile(unsigned int Device_Id)
{
	if (Device_Id<=0)
	{
		return 0;
	}
	char			*buff = NULL;
	int				buff_len = 0;
	int				Ret = 0;
	int				i = 0;
	int				newAreaNum = 0;
	rDevModel_SetArea	rAreaInfo;
	bool			bRet = false;
	QString			SectName;
	QString			KeyName;
	QString			strValue;

	SectName = QString::fromLocal8Bit("定值区");

	QFile			writeFile(m_ModelAllFileName);
	QTextStream		writeStream;

	qint64			KeyPos = 0;

	writeStream.setCodec("gbk");

	if(QFile::exists(m_ModelAllFileName))
	{
		if(!writeFile.open(QFile::ReadWrite | QIODevice::Text))
		{
			QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("无法打开指定文件!"), QMessageBox::Ok);
			return -1;
		}
		if(!SectName.isEmpty())
		{
			writeStream.setDevice(&writeFile);	
			KeyPos = writeFile.size();
			writeStream.seek(KeyPos);
			writeStream<<"["<<SectName<<"] "<<"\n";
			writeStream.flush();//写入流到文件
			writeFile.close();
		}
		else
		{
			writeFile.close();
			return -3;
		}
	}
	else
	{
		return -2;
	}

	Ret = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVMODEL_SETAREA, Device_Id+TABLE_RDEVMODEL_INFO*PARENT_OFFSET, buff, &buff_len, true);
	if (Ret>0 && buff)
	{
		for (i=0; i<Ret; i++)
		{
			memcpy(&rAreaInfo, buff+i*sizeof(rDevModel_SetArea), sizeof(rDevModel_SetArea));
			memset(m_strBuff, 0, TEMP_BUFF_LEN);
			sprintf(m_strBuff, "%d,%s,%d,%d ", 
				rAreaInfo.IndexNo,
				rAreaInfo.Name,						//名称
				rAreaInfo.Fun,						//FUN		
				rAreaInfo.Inf);

			strValue = QString::fromLocal8Bit(m_strBuff);
			KeyName = QString("%1").arg(i+1);
			bRet = m_FileOper.WritePrivateProfileString_My(SectName, KeyName, strValue, m_ModelAllFileName);
			if (!bRet)
			{
				break;
			}
			newAreaNum++;
		}
	}

	if (buff)
	{
		delete [](char*)buff;
		buff = NULL;
	}
	return newAreaNum;
}
